package com.ternnetwork.oa.controller.work;

import com.ternnetwork.commons.model.http.BaseResponse;
import com.ternnetwork.commons.utils.JsonUtils;
import com.ternnetwork.oa.dto.work.ReportAddDto;
import com.ternnetwork.oa.dto.work.ReportEditDto;
import com.ternnetwork.oa.service.work.ReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.security.Principal;


@Controller
@RequestMapping("/work/report/*")
public class ReportController {

    @Autowired
    private ReportService reportService;


    @PreAuthorize("hasAnyAuthority('API_APP_OA_WORK_REPORT_ADD')")
    @RequestMapping("/add")
    public @ResponseBody BaseResponse add(String requestJson, Principal user) throws Exception {
        ReportAddDto dto= JsonUtils.jsonToObjectThrowsException(requestJson, ReportAddDto.class);
        return reportService.add(dto,user.getName());
    }
    @PreAuthorize("hasAnyAuthority('API_APP_OA_WORK_REPORT_UPDATE')")
    @RequestMapping("/update")
    public @ResponseBody BaseResponse update(String requestJson, Principal user) throws Exception {
        ReportEditDto dto= JsonUtils.jsonToObjectThrowsException(requestJson, ReportEditDto.class);
        return reportService.update(dto,user.getName());
    }
    @PreAuthorize("hasAnyAuthority('API_APP_OA_WORK_REPORT_FIND_MY_ALL')")
    @RequestMapping("/findMyAll")
    public @ResponseBody BaseResponse findMyAll(String start,String end, Principal user)  {
      return reportService.findAll(start,end,user.getName());
    }
    @PreAuthorize("hasAnyAuthority('API_APP_OA_WORK_REPORT_FIND_ALL_BY_ID')")
    @RequestMapping("/findAllById")
    public @ResponseBody BaseResponse findAllById(String start,String end, Long userId)  {
        return reportService.findAll(start,end,userId);
    }
    @PreAuthorize("hasAnyAuthority('API_APP_OA_WORK_REPORT_TO_ME_FIND_ALL_BY_ID')")
    @RequestMapping("/2me/findAllById")
    public @ResponseBody BaseResponse toMeFindAllById(String start,String end, Long userId,Principal user)  {
        return reportService.toMeFindAll(start,end,userId,user.getName());
    }

    @PreAuthorize("hasAnyAuthority('API_APP_OA_WORK_REPORT_DELETE_BY_ID')")
    @RequestMapping("/deleteById")
    public @ResponseBody BaseResponse deleteById(Long id, Principal user) {
        return reportService.deleteById(id,user.getName());
    }



}
